using System;
using UnityEngine;

[Serializable]
public class Joystick : MonoBehaviour
{
	private bool touchPad;

	private Rect touchZone;

	public Vector2 deadZone;

	public Vector2 position;

	private int lastFingerId;

	private float tapTimeWindow;

	private Vector2 fingerDownPos;

	private float fingerDownTime;

	private float firstDeltaTime;

	private GUITexture gui;

	private GUITexture guiPad;

	private Rect defaultRect;

	private Vector2 guiTouchOffset;

	private Vector2 guiCenter;

	private Vector2 pivot;

	public spin Bey;

	public Joystick()
	{
		touchPad = true;
		deadZone = Vector2.zero;
		lastFingerId = -1;
		firstDeltaTime = 0.5f;
	}

	public void Start()
	{
		gui = (((GUITexture)GetComponent(typeof(GUITexture))) as GUITexture);
		guiPad = (((GUITexture)transform.GetChild(0).GetComponent(typeof(GUITexture))) as GUITexture);
		if (PlayerPrefs.GetInt("TouchControl") == 0)
		{
			UnityEngine.Object.Destroy(gameObject);
		}
		if (PlayerPrefs.GetInt("TouchControl") == 1)
		{
			touchPad = true;
		}
		if (PlayerPrefs.GetInt("TouchControl") == 2)
		{
			touchPad = false;
			transform.position = new Vector3(0.83f, 0.15f + (float)Screen.width * 4E-05f, 0f);
			if (Screen.width < 500)
			{
				transform.position = new Vector3(0.78f, 0.12f, 0f);
			}
			if (Screen.width > 1200)
			{
				gui.pixelInset = new Rect(guiCenter.x - 25f - (float)Screen.width * 0.015f * 0.5f, guiCenter.y - 25f - (float)Screen.width * 0.015f * 0.5f, 50f + (float)Screen.width * 0.015f, 50f + (float)Screen.width * 0.015f);
			}
		}
		if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Contains("MP"))
		{
			Invoke("find", 0.2f);
		}
		int num = 0;
		Color color = gui.color;
		color.a = num;
		Color color3 = gui.color = color;
		if (touchPad)
		{
			defaultRect = new Rect(-(20 + Screen.width), 0f, 20 + Screen.width, Screen.height);
			ref Rect reference = ref defaultRect;
			float x = defaultRect.x;
			Vector3 vector = transform.position;
			reference.x = x + vector.x * (float)Screen.width;
			ref Rect reference2 = ref defaultRect;
			float y = defaultRect.y;
			Vector3 vector2 = transform.position;
			reference2.y = y + vector2.y * (float)Screen.height;
			float x2 = 0f;
			Vector3 vector3 = transform.position;
			vector3.x = x2;
			Vector3 vector5 = transform.position = vector3;
			float y2 = 0f;
			Vector3 vector6 = transform.position;
			vector6.y = y2;
			Vector3 vector8 = transform.position = vector6;
			touchZone = defaultRect;
		}
		else
		{
			defaultRect = gui.pixelInset;
			ref Rect reference3 = ref defaultRect;
			float x3 = defaultRect.x;
			Vector3 vector9 = transform.position;
			reference3.x = x3 + vector9.x * (float)Screen.width;
			ref Rect reference4 = ref defaultRect;
			float y3 = defaultRect.y;
			Vector3 vector10 = transform.position;
			reference4.y = y3 + vector10.y * (float)Screen.height;
			float x4 = 0f;
			Vector3 vector11 = transform.position;
			vector11.x = x4;
			Vector3 vector13 = transform.position = vector11;
			float y4 = 0f;
			Vector3 vector14 = transform.position;
			vector14.y = y4;
			Vector3 vector16 = transform.position = vector14;
			guiTouchOffset.x = defaultRect.width * 0.5f;
			guiTouchOffset.y = defaultRect.height * 0.5f;
			guiCenter.x = defaultRect.x + guiTouchOffset.x;
			guiCenter.y = defaultRect.y + guiTouchOffset.y;
			guiPad.pixelInset = new Rect(guiCenter.x - 70f, guiCenter.y - 70f, 140f, 140f);
			if (Screen.width > 1200)
			{
				guiPad.pixelInset = new Rect(guiCenter.x - 70f - (float)Screen.width * 0.015f * 0.5f, guiCenter.y - 70f - (float)Screen.width * 0.015f * 0.5f, 140f + (float)Screen.width * 0.015f, 140f + (float)Screen.width * 0.015f);
			}
			pivot = new Vector2(defaultRect.x, defaultRect.y);
		}
		if (!touchPad)
		{
			float a = 0.4f;
			Color color4 = gui.color;
			color4.a = a;
			Color color6 = gui.color = color4;
		}
		else
		{
			UnityEngine.Object.Destroy(gui);
		}
	}

	public void Disable()
	{
		gameObject.SetActive(false);
	}

	public void ResetJoystick()
	{
		lastFingerId = -1;
		position = Vector2.zero;
		Vector2 zero = Vector2.zero;
		if (!touchPad)
		{
			gui.pixelInset = defaultRect;
			fingerDownPos = Vector2.zero;
		}
	}

	public bool IsFingerDown()
	{
		return lastFingerId != -1;
	}

	public void LatchedFinger(int fingerId)
	{
		if (lastFingerId == fingerId)
		{
			ResetJoystick();
		}
	}

	public void Update()
	{
		int touchCount = UnityEngine.Input.touchCount;
		if (touchCount == 0)
		{
			ResetJoystick();
		}
		else
		{
			for (int i = 0; i < touchCount; i++)
			{
				Touch touch = UnityEngine.Input.GetTouch(i);
				Vector2 vector = touch.position - guiTouchOffset;
				bool flag = false;
				if (touchPad)
				{
					if (touchZone.Contains(touch.position))
					{
						flag = true;
					}
				}
				else if (guiPad.HitTest(touch.position))
				{
					flag = true;
				}
				if (flag && (lastFingerId == -1 || lastFingerId != touch.fingerId))
				{
					if (touchPad)
					{
						lastFingerId = touch.fingerId;
						fingerDownPos = touch.position;
						fingerDownTime = Time.time;
					}
					lastFingerId = touch.fingerId;
				}
				if (lastFingerId == touch.fingerId)
				{
					if (touchPad)
					{
						ref Vector2 reference = ref position;
						Vector2 vector2 = touch.position;
						reference.x = Mathf.Clamp((vector2.x - fingerDownPos.x) / (touchZone.width / 2f), -1f, 1f);
						ref Vector2 reference2 = ref position;
						Vector2 vector3 = touch.position;
						reference2.y = Mathf.Clamp((vector3.y - fingerDownPos.y) / (touchZone.height / 2f), -1f, 1f);
					}
					else
					{
						float x = vector.x;
						Rect pixelInset = gui.pixelInset;
						float num2 = pixelInset.x = x;
						Rect rect2 = gui.pixelInset = pixelInset;
						float y = vector.y;
						Rect pixelInset2 = gui.pixelInset;
						float num4 = pixelInset2.y = y;
						Rect rect4 = gui.pixelInset = pixelInset2;
						Vector2 b = default(Vector2);
						Vector2 vector4 = default(Vector2);
						b.x = gui.pixelInset.x;
						b.y = gui.pixelInset.y;
						vector4 = pivot - b;
						if (!(vector4.sqrMagnitude <= Mathf.Pow(65f + (float)Screen.width * 0.015f, 2f)))
						{
							b = pivot - vector4.normalized * 65f;
						}
						float x2 = b.x;
						Rect pixelInset3 = gui.pixelInset;
						float num6 = pixelInset3.x = x2;
						Rect rect6 = gui.pixelInset = pixelInset3;
						float y2 = b.y;
						Rect pixelInset4 = gui.pixelInset;
						float num8 = pixelInset4.y = y2;
						Rect rect8 = gui.pixelInset = pixelInset4;
					}
				}
				if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
				{
					ResetJoystick();
				}
			}
		}
		if (!touchPad)
		{
			position.x = (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x) / guiTouchOffset.x * 0.2f;
			position.y = (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y) / guiTouchOffset.y * 0.2f;
		}
		float num9 = Mathf.Abs(position.x);
		float num10 = Mathf.Abs(position.y);
		if (!(num9 >= deadZone.x))
		{
			position.x = 0f;
		}
		if (!(num10 >= deadZone.y))
		{
			position.y = 0f;
		}
		Bey.touchx = position.x * 1.5f;
		Bey.touchz = position.y * 1.5f;
	}

	public void find()
	{
		Bey = (((spin)GameObject.Find("PlayerPrefab(Clone)").GetComponent(typeof(spin))) as spin);
	}

	public void Main()
	{
	}
}
